package com.utsirc.irc;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;

import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class IRCClient extends PircBot {

	public String channel;
	public Messenger messenger;

	public IRCClient(Messenger messenger) {
		this.messenger = messenger;
	}
	
	public void setChannel(String channel) {
		if (!channel.equals(this.channel)) {
			this.channel = channel;
			this.joinChannel(this.channel);
		}
	}

	public void setNick(String nick) {
		this.setName(nick);
	}

	public void sendMessage(String message) {
		super.sendMessage(this.channel, message);
	}

	@Override
	protected void onConnect() {

		Map<String, String> params = new HashMap<String, String>();
		params.put("nick", this.getNick());

		try {
			this.notifyHandler("CONNECTED", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onChannelInfo(String channel, int userCount, String topic) {

		Bundle mBundle = new Bundle();
		mBundle.putString("channel", channel);
		mBundle.putInt("userCount", userCount);
		mBundle.putString("topic", topic);
		
		Message msg = Message.obtain();
		msg.obj = "CHANNEL";
		msg.setData(mBundle);
		
		try {
			this.messenger.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onJoin(String channel, String sender, String login,
			String hostname) {

		Map<String, String> params = new HashMap<String, String>();
		params.put("channel", channel);
		params.put("sender", sender);
		params.put("login", login);
		params.put("hostname", hostname);
		
		try {
			this.notifyHandler("JOINED", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onUserList(String channel, User[] users) {
		
		Bundle mBundle = new Bundle();
		mBundle.putString("channel", channel);
		
//		since bundle cannot store custom obj
//		splits the obj array into multiple type arrays
		String[] nicks = new String[users.length];
		String[] prefixes = new String[users.length];
		for (int i = 0; i < users.length; i++) {
			nicks[i] = users[i].getNick();
			prefixes[i] = users[i].getPrefix();
		}
		mBundle.putStringArray("nicks", nicks);
		mBundle.putStringArray("prefixes", prefixes);
		
		Message msg = Message.obtain();
		msg.obj = "USERLIST";
		msg.setData(mBundle);
		
		try {
			this.messenger.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onMessage(String channel, String sender, String login,
			String hostname, String message) {

		Map<String, String> params = new HashMap<String, String>();
		params.put("channel", channel);
		params.put("sender", sender);
		params.put("login", login);
		params.put("hostname", hostname);
		params.put("message", message);

		try {
			this.notifyHandler("RECIEVED", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onAction(String sender, String login, String hostname,
			String target, String action) {
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("sender", sender);
		params.put("login", login);
		params.put("hostname", hostname);
		params.put("target", target);
		params.put("action", action);
		
		try {
			this.notifyHandler("ACTION", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onPart(String channel, String sender, String login,
			String hostname) {
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("channel", channel);
		params.put("sender", sender);
		params.put("login", login);
		params.put("hostname", hostname);
		
		try {
			this.notifyHandler("PART", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDisconnect() {
		
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			this.notifyHandler("DISCONNECT", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onQuit(String sourceNick, String sourceLogin,
			String sourceHostname, String reason) {
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("sourceNick", sourceNick);
		params.put("sourceLogin", sourceLogin);
		params.put("sourceHostname", sourceHostname);
		params.put("reason", reason);
		
		try {
			this.notifyHandler("QUIT", params);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	// notify the handler on irc state change
	private void notifyHandler(String message, Map<String, String> params)
			throws RemoteException {

		Bundle mBundle = new Bundle();
		for (Entry<String, String> param : params.entrySet()) {
			mBundle.putString(param.getKey(), param.getValue());
		}
		Message msg = Message.obtain();
		msg.obj = message;
		msg.setData(mBundle);
		this.messenger.send(msg);
	}
}
